<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>libfuse: example/notify_store_retrieve.c File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">libfuse
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',false,false,'search.php','Search');
});
</script>
<div id="main-nav"></div>
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_cfafba98a580ce4b62f8a6fa96d7cbb0.html">example</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">notify_store_retrieve.c File Reference</div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><code>#include &lt;<a class="el" href="fuse__lowlevel_8h_source.html">fuse_lowlevel.h</a>&gt;</code><br />
<code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;string.h&gt;</code><br />
<code>#include &lt;errno.h&gt;</code><br />
<code>#include &lt;fcntl.h&gt;</code><br />
<code>#include &lt;assert.h&gt;</code><br />
<code>#include &lt;stddef.h&gt;</code><br />
<code>#include &lt;unistd.h&gt;</code><br />
<code>#include &lt;pthread.h&gt;</code><br />
</div>
<p><a href="notify__store__retrieve_8c_source.html">Go to the source code of this file.</a></p>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>This example implements a file system with a single file whose contents change dynamically: it always contains the current time.</p>
<p>While <a class="el" href="notify__inval__inode_8c.html">notify_inval_inode.c</a> uses <a class="el" href="fuse__lowlevel_8h.html#a9cb974af9745294ff446d11cba2422f1">fuse_lowlevel_notify_inval_inode()</a> to let the kernel know that it has to invalidate the cache, this example actively pushes the updated data into the kernel cache using <a class="el" href="fuse__lowlevel_8h.html#af856725ed4a13ed7c17512554043edbc">fuse_lowlevel_notify_store()</a>.</p>
<p>To see the effect, first start the file system with the <code>--no-notify</code> option: </p><pre class="fragment">$ notify_store_retrieve --update-interval=1 --no-notify mnt/
</pre><p>Observe that the output never changes, even though the file system updates it once per second. This is because the contents are cached in the kernel: </p><pre class="fragment">$ for i in 1 2 3 4 5; do
&gt;     cat mnt/current_time
&gt;     sleep 1
&gt; done
The current time is 15:58:18
The current time is 15:58:18
The current time is 15:58:18
The current time is 15:58:18
The current time is 15:58:18
</pre><p>If you instead enable the notification functions, the changes become visible: </p><pre class="fragment">$ notify_store_retrieve --update-interval=1 mnt/
$ for i in 1 2 3 4 5; do
&gt;     cat mnt/current_time
&gt;     sleep 1
&gt; done
The current time is 15:58:40
The current time is 15:58:41
The current time is 15:58:42
The current time is 15:58:43
The current time is 15:58:44
</pre><h2>Compilation</h2>
<pre class="fragment">gcc -Wall notify_store_retrieve.c `pkg-config fuse3 --cflags --libs` -o notify_store_retrieve
</pre><h2>Source code</h2>
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">  FUSE: Filesystem in Userspace</span></div><div class="line"><span class="comment">  Copyright (C) 2016 Nikolaus Rath &lt;Nikolaus@rath.org&gt;</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  This program can be distributed under the terms of the GNU GPL.</span></div><div class="line"><span class="comment">  See the file COPYING.</span></div><div class="line"><span class="comment">*/</span></div><div class="line"></div><div class="line"><span class="preprocessor">#define FUSE_USE_VERSION 31</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="fuse__lowlevel_8h.html">fuse_lowlevel.h</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;string.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;errno.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;fcntl.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;assert.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stddef.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;unistd.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;pthread.h&gt;</span></div><div class="line"></div><div class="line"><span class="comment">/* We can&#39;t actually tell the kernel that there is no</span></div><div class="line"><span class="comment">   timeout, so we just send a big value */</span></div><div class="line"><span class="preprocessor">#define NO_TIMEOUT 500000</span></div><div class="line"></div><div class="line"><span class="comment">/* We cannot check directly if e.g. O_RDONLY is set, since this is not</span></div><div class="line"><span class="comment"> * an individual bit (cf. open(2)) */</span></div><div class="line"><span class="preprocessor">#define ACCESS_MASK (O_RDONLY | O_WRONLY | O_RDWR)</span></div><div class="line"></div><div class="line"><span class="preprocessor">#define MAX_STR_LEN 128</span></div><div class="line"><span class="preprocessor">#define FILE_INO 2</span></div><div class="line"><span class="preprocessor">#define FILE_NAME &quot;current_time&quot;</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">char</span> file_contents[MAX_STR_LEN];</div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> lookup_cnt = 0;</div><div class="line"><span class="keyword">static</span> <span class="keywordtype">size_t</span> file_size;</div><div class="line"></div><div class="line"><span class="comment">/* Keep track if we ever stored data (==1), and</span></div><div class="line"><span class="comment">   received it back correctly (==2) */</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> retrieve_status = 0;</div><div class="line"></div><div class="line"><span class="comment">/* Command line parsing */</span></div><div class="line"><span class="keyword">struct </span>options {</div><div class="line">    <span class="keywordtype">int</span> no_notify;</div><div class="line">    <span class="keywordtype">int</span> update_interval;</div><div class="line">};</div><div class="line"><span class="keyword">static</span> <span class="keyword">struct </span>options options = {</div><div class="line">    .no_notify = 0,</div><div class="line">    .update_interval = 1,</div><div class="line">};</div><div class="line"></div><div class="line"><span class="preprocessor">#define OPTION(t, p)                           \</span></div><div class="line"><span class="preprocessor">    { t, offsetof(struct options, p), 1 }</span></div><div class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">struct </span><a class="code" href="structfuse__opt.html">fuse_opt</a> option_spec[] = {</div><div class="line">    OPTION(<span class="stringliteral">&quot;--no-notify&quot;</span>, no_notify),</div><div class="line">    OPTION(<span class="stringliteral">&quot;--update-interval=%d&quot;</span>, update_interval),</div><div class="line">    <a class="code" href="fuse__opt_8h.html#aca35962e17d189ceb5447f8eea11bd33">FUSE_OPT_END</a></div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> tfs_stat(<a class="code" href="fuse__lowlevel_8h.html#ad119a72f00b4cd2e4a500fd3364ae1e2">fuse_ino_t</a> ino, <span class="keyword">struct</span> stat *stbuf) {</div><div class="line">    stbuf-&gt;st_ino = ino;</div><div class="line">    <span class="keywordflow">if</span> (ino == <a class="code" href="fuse__lowlevel_8h.html#a14d7299559cf05272b838cfc6388ef91">FUSE_ROOT_ID</a>) {</div><div class="line">        stbuf-&gt;st_mode = S_IFDIR | 0755;</div><div class="line">        stbuf-&gt;st_nlink = 1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ino == FILE_INO) {</div><div class="line">        stbuf-&gt;st_mode = S_IFREG | 0444;</div><div class="line">        stbuf-&gt;st_nlink = 1;</div><div class="line">        stbuf-&gt;st_size = file_size;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> tfs_lookup(<a class="code" href="fuse__lowlevel_8h.html#a33e2aa4a8905a05397292ae047cd2257">fuse_req_t</a> req, <a class="code" href="fuse__lowlevel_8h.html#ad119a72f00b4cd2e4a500fd3364ae1e2">fuse_ino_t</a> parent,</div><div class="line">                       <span class="keyword">const</span> <span class="keywordtype">char</span> *name) {</div><div class="line">    <span class="keyword">struct </span><a class="code" href="structfuse__entry__param.html">fuse_entry_param</a> e;</div><div class="line">    memset(&amp;e, 0, <span class="keyword">sizeof</span>(e));</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (parent != <a class="code" href="fuse__lowlevel_8h.html#a14d7299559cf05272b838cfc6388ef91">FUSE_ROOT_ID</a>)</div><div class="line">        <span class="keywordflow">goto</span> err_out;</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(name, FILE_NAME) == 0) {</div><div class="line">        e.ino = FILE_INO;</div><div class="line">        lookup_cnt++;</div><div class="line">    } <span class="keywordflow">else</span></div><div class="line">        <span class="keywordflow">goto</span> err_out;</div><div class="line"></div><div class="line">    e.attr_timeout = NO_TIMEOUT;</div><div class="line">    e.entry_timeout = NO_TIMEOUT;</div><div class="line">    <span class="keywordflow">if</span> (tfs_stat(e.ino, &amp;e.attr) != 0)</div><div class="line">        <span class="keywordflow">goto</span> err_out;</div><div class="line">    <a class="code" href="fuse__lowlevel_8h.html#a672c45e126cd240f4bcd59bf9b7e3708">fuse_reply_entry</a>(req, &amp;e);</div><div class="line">    <span class="keywordflow">return</span>;</div><div class="line"></div><div class="line">err_out:</div><div class="line">    <a class="code" href="fuse__lowlevel_8h.html#a2553c03f9a63c75e609e67f90a3a5d88">fuse_reply_err</a>(req, ENOENT);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> tfs_forget (<a class="code" href="fuse__lowlevel_8h.html#a33e2aa4a8905a05397292ae047cd2257">fuse_req_t</a> req, <a class="code" href="fuse__lowlevel_8h.html#ad119a72f00b4cd2e4a500fd3364ae1e2">fuse_ino_t</a> ino,</div><div class="line">                        uint64_t nlookup) {</div><div class="line">    (void) req;</div><div class="line">    <span class="keywordflow">if</span>(ino == FILE_INO)</div><div class="line">        lookup_cnt -= nlookup;</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        assert(ino == <a class="code" href="fuse__lowlevel_8h.html#a14d7299559cf05272b838cfc6388ef91">FUSE_ROOT_ID</a>);</div><div class="line">    <a class="code" href="fuse__lowlevel_8h.html#a85ae91390a6704dc26f8d80fed7d5678">fuse_reply_none</a>(req);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> tfs_getattr(<a class="code" href="fuse__lowlevel_8h.html#a33e2aa4a8905a05397292ae047cd2257">fuse_req_t</a> req, <a class="code" href="fuse__lowlevel_8h.html#ad119a72f00b4cd2e4a500fd3364ae1e2">fuse_ino_t</a> ino,</div><div class="line">                        <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi) {</div><div class="line">    <span class="keyword">struct </span>stat stbuf;</div><div class="line"></div><div class="line">    (void) fi;</div><div class="line"></div><div class="line">    memset(&amp;stbuf, 0, <span class="keyword">sizeof</span>(stbuf));</div><div class="line">    <span class="keywordflow">if</span> (tfs_stat(ino, &amp;stbuf) != 0)</div><div class="line">        <a class="code" href="fuse__lowlevel_8h.html#a2553c03f9a63c75e609e67f90a3a5d88">fuse_reply_err</a>(req, ENOENT);</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        <a class="code" href="fuse__lowlevel_8h.html#ad28378dc569019c32acdb4995d70be18">fuse_reply_attr</a>(req, &amp;stbuf, NO_TIMEOUT);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">struct </span>dirbuf {</div><div class="line">    <span class="keywordtype">char</span> *p;</div><div class="line">    <span class="keywordtype">size_t</span> size;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> dirbuf_add(<a class="code" href="fuse__lowlevel_8h.html#a33e2aa4a8905a05397292ae047cd2257">fuse_req_t</a> req, <span class="keyword">struct</span> dirbuf *b, <span class="keyword">const</span> <span class="keywordtype">char</span> *name,</div><div class="line">                       <a class="code" href="fuse__lowlevel_8h.html#ad119a72f00b4cd2e4a500fd3364ae1e2">fuse_ino_t</a> ino) {</div><div class="line">    <span class="keyword">struct </span>stat stbuf;</div><div class="line">    <span class="keywordtype">size_t</span> oldsize = b-&gt;size;</div><div class="line">    b-&gt;size += <a class="code" href="fuse__lowlevel_8h.html#ad1957bcc8ece8c90f16c42c4daf3053f">fuse_add_direntry</a>(req, NULL, 0, name, NULL, 0);</div><div class="line">    b-&gt;p = (<span class="keywordtype">char</span> *) realloc(b-&gt;p, b-&gt;size);</div><div class="line">    memset(&amp;stbuf, 0, <span class="keyword">sizeof</span>(stbuf));</div><div class="line">    stbuf.st_ino = ino;</div><div class="line">    <a class="code" href="fuse__lowlevel_8h.html#ad1957bcc8ece8c90f16c42c4daf3053f">fuse_add_direntry</a>(req, b-&gt;p + oldsize, b-&gt;size - oldsize, name, &amp;stbuf,</div><div class="line">                      b-&gt;size);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="preprocessor">#define min(x, y) ((x) &lt; (y) ? (x) : (y))</span></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> reply_buf_limited(<a class="code" href="fuse__lowlevel_8h.html#a33e2aa4a8905a05397292ae047cd2257">fuse_req_t</a> req, <span class="keyword">const</span> <span class="keywordtype">char</span> *buf, <span class="keywordtype">size_t</span> bufsize,</div><div class="line">                             off_t off, <span class="keywordtype">size_t</span> maxsize) {</div><div class="line">    <span class="keywordflow">if</span> (off &lt; bufsize)</div><div class="line">        <span class="keywordflow">return</span> <a class="code" href="fuse__lowlevel_8h.html#a300a88b63ab7c8ca92853a97486448c0">fuse_reply_buf</a>(req, buf + off,</div><div class="line">                              min(bufsize - off, maxsize));</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        <span class="keywordflow">return</span> <a class="code" href="fuse__lowlevel_8h.html#a300a88b63ab7c8ca92853a97486448c0">fuse_reply_buf</a>(req, NULL, 0);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> tfs_readdir(<a class="code" href="fuse__lowlevel_8h.html#a33e2aa4a8905a05397292ae047cd2257">fuse_req_t</a> req, <a class="code" href="fuse__lowlevel_8h.html#ad119a72f00b4cd2e4a500fd3364ae1e2">fuse_ino_t</a> ino, <span class="keywordtype">size_t</span> size,</div><div class="line">                        off_t off, <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi) {</div><div class="line">    (void) fi;</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (ino != <a class="code" href="fuse__lowlevel_8h.html#a14d7299559cf05272b838cfc6388ef91">FUSE_ROOT_ID</a>)</div><div class="line">        <a class="code" href="fuse__lowlevel_8h.html#a2553c03f9a63c75e609e67f90a3a5d88">fuse_reply_err</a>(req, ENOTDIR);</div><div class="line">    <span class="keywordflow">else</span> {</div><div class="line">        <span class="keyword">struct </span>dirbuf b;</div><div class="line"></div><div class="line">        memset(&amp;b, 0, <span class="keyword">sizeof</span>(b));</div><div class="line">        dirbuf_add(req, &amp;b, FILE_NAME, FILE_INO);</div><div class="line">        reply_buf_limited(req, b.p, b.size, off, size);</div><div class="line">        free(b.p);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> tfs_open(<a class="code" href="fuse__lowlevel_8h.html#a33e2aa4a8905a05397292ae047cd2257">fuse_req_t</a> req, <a class="code" href="fuse__lowlevel_8h.html#ad119a72f00b4cd2e4a500fd3364ae1e2">fuse_ino_t</a> ino,</div><div class="line">                     <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi) {</div><div class="line"></div><div class="line">    <span class="comment">/* Make cache persistent even if file is closed,</span></div><div class="line"><span class="comment">       this makes it easier to see the effects */</span></div><div class="line">    fi-&gt;<a class="code" href="structfuse__file__info.html#a23a64eaecbf83f99aba8ee79e6de2780">keep_cache</a> = 1;</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (ino == <a class="code" href="fuse__lowlevel_8h.html#a14d7299559cf05272b838cfc6388ef91">FUSE_ROOT_ID</a>)</div><div class="line">        <a class="code" href="fuse__lowlevel_8h.html#a2553c03f9a63c75e609e67f90a3a5d88">fuse_reply_err</a>(req, EISDIR);</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> ((fi-&gt;<a class="code" href="structfuse__file__info.html#ae7d31802727be19670193a411647bca5">flags</a> &amp; ACCESS_MASK) != O_RDONLY)</div><div class="line">        <a class="code" href="fuse__lowlevel_8h.html#a2553c03f9a63c75e609e67f90a3a5d88">fuse_reply_err</a>(req, EACCES);</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ino == FILE_INO)</div><div class="line">        <a class="code" href="fuse__lowlevel_8h.html#a170f8c6b953d70928e83bcecee43bfdc">fuse_reply_open</a>(req, fi);</div><div class="line">    <span class="keywordflow">else</span> {</div><div class="line">        <span class="comment">// This should not happen</span></div><div class="line">        fprintf(stderr, <span class="stringliteral">&quot;Got open for non-existing inode!\n&quot;</span>);</div><div class="line">        <a class="code" href="fuse__lowlevel_8h.html#a2553c03f9a63c75e609e67f90a3a5d88">fuse_reply_err</a>(req, ENOENT);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> tfs_read(<a class="code" href="fuse__lowlevel_8h.html#a33e2aa4a8905a05397292ae047cd2257">fuse_req_t</a> req, <a class="code" href="fuse__lowlevel_8h.html#ad119a72f00b4cd2e4a500fd3364ae1e2">fuse_ino_t</a> ino, <span class="keywordtype">size_t</span> size,</div><div class="line">                     off_t off, <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi) {</div><div class="line">    (void) fi;</div><div class="line"></div><div class="line">    assert(ino == FILE_INO);</div><div class="line">    reply_buf_limited(req, file_contents, file_size, off, size);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> tfs_retrieve_reply(<a class="code" href="fuse__lowlevel_8h.html#a33e2aa4a8905a05397292ae047cd2257">fuse_req_t</a> req, <span class="keywordtype">void</span> *cookie, <a class="code" href="fuse__lowlevel_8h.html#ad119a72f00b4cd2e4a500fd3364ae1e2">fuse_ino_t</a> ino,</div><div class="line">                               off_t offset, <span class="keyword">struct</span> <a class="code" href="structfuse__bufvec.html">fuse_bufvec</a> *data) {</div><div class="line">    <span class="keyword">struct </span><a class="code" href="structfuse__bufvec.html">fuse_bufvec</a> bufv;</div><div class="line">    <span class="keywordtype">char</span> buf[MAX_STR_LEN];</div><div class="line">    <span class="keywordtype">char</span> *expected;</div><div class="line">    ssize_t ret;</div><div class="line"></div><div class="line">    assert(ino == FILE_INO);</div><div class="line">    assert(offset == 0);</div><div class="line">    expected = (<span class="keywordtype">char</span>*) cookie;</div><div class="line"></div><div class="line">    bufv.count = 1;</div><div class="line">    bufv.idx = 0;</div><div class="line">    bufv.off = 0;</div><div class="line">    bufv.buf[0].size = MAX_STR_LEN;</div><div class="line">    bufv.buf[0].mem = <a class="code" href="structfuse__bufvec.html#a754caca61fdf8107d28b510505a033bb">buf</a>;</div><div class="line">    bufv.buf[0].<a class="code" href="structfuse__buf.html#a1928e204554f2d37cb8dac28a8a2f28c">flags</a> = 0;</div><div class="line"></div><div class="line">    ret = <a class="code" href="fuse__common_8h.html#a9ca301390fb5e85b85153abb1891a3f7">fuse_buf_copy</a>(&amp;bufv, data, 0);</div><div class="line">    assert(ret &gt; 0);</div><div class="line">    assert(strncmp(buf, expected, ret) == 0);</div><div class="line">    free(expected);</div><div class="line">    retrieve_status = 2;</div><div class="line">    <a class="code" href="fuse__lowlevel_8h.html#a85ae91390a6704dc26f8d80fed7d5678">fuse_reply_none</a>(req);</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keyword">struct </span><a class="code" href="structfuse__lowlevel__ops.html">fuse_lowlevel_ops</a> tfs_oper = {</div><div class="line">    .<a class="code" href="structfuse__lowlevel__ops.html#a7f46abd7c2795cfcf7a3d8c5ee8f6c70">lookup</a>     = tfs_lookup,</div><div class="line">    .getattr    = tfs_getattr,</div><div class="line">    .readdir    = tfs_readdir,</div><div class="line">    .open       = tfs_open,</div><div class="line">    .read       = tfs_read,</div><div class="line">    .forget     = tfs_forget,</div><div class="line">    .retrieve_reply = tfs_retrieve_reply,</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> update_fs(<span class="keywordtype">void</span>) {</div><div class="line">    <span class="keyword">struct </span>tm *now;</div><div class="line">    time_t t;</div><div class="line">    t = time(NULL);</div><div class="line">    now = localtime(&amp;t);</div><div class="line">    assert(now != NULL);</div><div class="line"></div><div class="line">    file_size = strftime(file_contents, MAX_STR_LEN,</div><div class="line">                         <span class="stringliteral">&quot;The current time is %H:%M:%S\n&quot;</span>, now);</div><div class="line">    assert(file_size != 0);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span>* update_fs_loop(<span class="keywordtype">void</span> *data) {</div><div class="line">    <span class="keyword">struct </span>fuse_session *se = (<span class="keyword">struct </span>fuse_session*) data;</div><div class="line">    <span class="keyword">struct </span><a class="code" href="structfuse__bufvec.html">fuse_bufvec</a> bufv;</div><div class="line">    <span class="keywordtype">int</span> ret;</div><div class="line"></div><div class="line">    <span class="keywordflow">while</span>(1) {</div><div class="line">        update_fs();</div><div class="line">        <span class="keywordflow">if</span> (!options.no_notify &amp;&amp; lookup_cnt) {</div><div class="line">            <span class="comment">/* Only send notification if the kernel</span></div><div class="line"><span class="comment">               is aware of the inode */</span></div><div class="line">            bufv.count = 1;</div><div class="line">            bufv.idx = 0;</div><div class="line">            bufv.off = 0;</div><div class="line">            bufv.buf[0].size = file_size;</div><div class="line">            bufv.buf[0].mem = file_contents;</div><div class="line">            bufv.buf[0].flags = 0;</div><div class="line"></div><div class="line">            <span class="comment">/* This shouldn&#39;t fail, but apparently it sometimes</span></div><div class="line"><span class="comment">               does - see https://github.com/libfuse/libfuse/issues/105 */</span></div><div class="line">            ret = <a class="code" href="fuse__lowlevel_8h.html#af856725ed4a13ed7c17512554043edbc">fuse_lowlevel_notify_store</a>(se, FILE_INO, 0, &amp;bufv, 0);</div><div class="line">            <span class="keywordflow">if</span> (-ret == ENODEV) {</div><div class="line">                <span class="comment">// File system was unmounted</span></div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line">            <span class="keywordflow">else</span> <span class="keywordflow">if</span> (ret != 0) {</div><div class="line">                fprintf(stderr, <span class="stringliteral">&quot;ERROR: fuse_lowlevel_notify_store() failed with %s (%d)\n&quot;</span>,</div><div class="line">                        strerror(-ret), -ret);</div><div class="line">                abort();</div><div class="line">            }</div><div class="line"></div><div class="line">            <span class="comment">/* To make sure that everything worked correctly, ask the</span></div><div class="line"><span class="comment">               kernel to send us back the stored data */</span></div><div class="line">            ret = <a class="code" href="fuse__lowlevel_8h.html#a58cb3543209d2c29dc2830f2503b5058">fuse_lowlevel_notify_retrieve</a>(se, FILE_INO, MAX_STR_LEN,</div><div class="line">                                                0, (<span class="keywordtype">void</span>*) strdup(file_contents));</div><div class="line">            <span class="keywordflow">if</span> (-ret == ENODEV) { <span class="comment">// File system was unmounted</span></div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">            }</div><div class="line">            assert(ret == 0);</div><div class="line">            <span class="keywordflow">if</span>(retrieve_status == 0)</div><div class="line">                retrieve_status = 1;</div><div class="line">        }</div><div class="line">        sleep(options.update_interval);</div><div class="line">    }</div><div class="line">    <span class="keywordflow">return</span> NULL;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> show_help(<span class="keyword">const</span> <span class="keywordtype">char</span> *progname)</div><div class="line">{</div><div class="line">    printf(<span class="stringliteral">&quot;usage: %s [options] &lt;mountpoint&gt;\n\n&quot;</span>, progname);</div><div class="line">    printf(<span class="stringliteral">&quot;File-system specific options:\n&quot;</span></div><div class="line">               <span class="stringliteral">&quot;    --update-interval=&lt;secs&gt;  Update-rate of file system contents\n&quot;</span></div><div class="line">               <span class="stringliteral">&quot;    --no-notify            Disable kernel notifications\n&quot;</span></div><div class="line">               <span class="stringliteral">&quot;\n&quot;</span>);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]) {</div><div class="line">    <span class="keyword">struct </span><a class="code" href="structfuse__args.html">fuse_args</a> args = <a class="code" href="fuse__opt_8h.html#a9bea40fe56b18be9aa110185ab7387ed">FUSE_ARGS_INIT</a>(argc, argv);</div><div class="line">    <span class="keyword">struct </span>fuse_session *se;</div><div class="line">    <span class="keyword">struct </span>fuse_cmdline_opts opts;</div><div class="line">    pthread_t updater;</div><div class="line">    <span class="keywordtype">int</span> ret = -1;</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (<a class="code" href="fuse__opt_8h.html#a539ef1f571c34f516c60c4cbe2901c0e">fuse_opt_parse</a>(&amp;args, &amp;options, option_spec, NULL) == -1)</div><div class="line">        <span class="keywordflow">return</span> 1;</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (<a class="code" href="fuse__lowlevel_8h.html#a8421a0cb3b6fb7013c7272c6997d1e08">fuse_parse_cmdline</a>(&amp;args, &amp;opts) != 0)</div><div class="line">        <span class="keywordflow">return</span> 1;</div><div class="line">    <span class="keywordflow">if</span> (opts.show_help) {</div><div class="line">        show_help(argv[0]);</div><div class="line">        <a class="code" href="fuse__lowlevel_8h.html#a7235e3a2d1c780d5e0beaee13c81529f">fuse_cmdline_help</a>();</div><div class="line">        <a class="code" href="fuse__lowlevel_8h.html#a990af0becaba1b5e45781d399720f85e">fuse_lowlevel_help</a>();</div><div class="line">        ret = 0;</div><div class="line">        <span class="keywordflow">goto</span> err_out1;</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (opts.show_version) {</div><div class="line">        printf(<span class="stringliteral">&quot;FUSE library version %s\n&quot;</span>, <a class="code" href="fuse__common_8h.html#ac52e27388a7c16eb509173908e5eebd0">fuse_pkgversion</a>());</div><div class="line">        <a class="code" href="fuse__lowlevel_8h.html#ac6e2d0fde62dcf4f0e57afeabeefd7b1">fuse_lowlevel_version</a>();</div><div class="line">        ret = 0;</div><div class="line">        <span class="keywordflow">goto</span> err_out1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">/* Initial contents */</span></div><div class="line">    update_fs();</div><div class="line"></div><div class="line">    se = <a class="code" href="fuse__lowlevel_8h.html#a9ee52f81d0c63d9bd46b11314ba596cf">fuse_session_new</a>(&amp;args, &amp;tfs_oper,</div><div class="line">                          <span class="keyword">sizeof</span>(tfs_oper), NULL);</div><div class="line">    <span class="keywordflow">if</span> (se == NULL)</div><div class="line">        <span class="keywordflow">goto</span> err_out1;</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (<a class="code" href="fuse__common_8h.html#a292dccc3c7b1799cb054efa2ba0c774b">fuse_set_signal_handlers</a>(se) != 0)</div><div class="line">        <span class="keywordflow">goto</span> err_out2;</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (<a class="code" href="fuse__lowlevel_8h.html#aa6d77679a110582684e9ca2da623bbc2">fuse_session_mount</a>(se, opts.mountpoint) != 0)</div><div class="line">        <span class="keywordflow">goto</span> err_out3;</div><div class="line"></div><div class="line">    <a class="code" href="fuse__common_8h.html#af1857d2209952f49a762aff39f3cd8bd">fuse_daemonize</a>(opts.foreground);</div><div class="line"></div><div class="line">    <span class="comment">/* Start thread to update file contents */</span></div><div class="line">    ret = pthread_create(&amp;updater, NULL, update_fs_loop, (<span class="keywordtype">void</span> *)se);</div><div class="line">    <span class="keywordflow">if</span> (ret != 0) {</div><div class="line">        fprintf(stderr, <span class="stringliteral">&quot;pthread_create failed with %s\n&quot;</span>,</div><div class="line">                strerror(ret));</div><div class="line">        <span class="keywordflow">goto</span> err_out3;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">/* Block until ctrl+c or fusermount -u */</span></div><div class="line">    <span class="keywordflow">if</span> (opts.singlethread)</div><div class="line">        ret = <a class="code" href="fuse__lowlevel_8h.html#a5f1e538aa3287e251afbe985438c4249">fuse_session_loop</a>(se);</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        ret = fuse_session_loop_mt(se, opts.clone_fd);</div><div class="line"></div><div class="line">    assert(retrieve_status != 1);</div><div class="line">    <a class="code" href="fuse__lowlevel_8h.html#a6c10d942751ddb214863a8b5e53de5e8">fuse_session_unmount</a>(se);</div><div class="line">err_out3:</div><div class="line">    <a class="code" href="fuse__common_8h.html#aeb674bbc11074c4fe520b952d6bfdd30">fuse_remove_signal_handlers</a>(se);</div><div class="line">err_out2:</div><div class="line">    <a class="code" href="fuse__lowlevel_8h.html#a08b5503c4e9656f9c4bc88331233cc65">fuse_session_destroy</a>(se);</div><div class="line">err_out1:</div><div class="line">    free(opts.mountpoint);</div><div class="line">    <a class="code" href="fuse__opt_8h.html#a2cdf272429ab3869a5162976141b287d">fuse_opt_free_args</a>(&amp;args);</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> ret ? 1 : 0;</div><div class="line">}</div><div class="line"></div><div class="line"></div></div><!-- fragment --> 
<p class="definition">Definition in file <a class="el" href="notify__store__retrieve_8c_source.html">notify_store_retrieve.c</a>.</p>
</div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>
